﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;

namespace OverSATbLib
{
    public class Range
    {
        public BigInteger Start;
        public BigInteger Stop;
        public bool Empty;

        public Range()
        {
            Start = new BigInteger(0);
            Stop = new BigInteger(0);
            Empty = true;
        }

        public Range(int start, int stop)
        {
            Start = start;
            Stop = stop;
            Empty = false;
        }

        public Range(BigInteger start, BigInteger stop)
        {
            Start = start;
            Stop = stop;
            Empty = false;
        }

        public bool Contains(BigInteger point)
        {
            return (point >= Start && point < Stop);
        }

        public bool Equals(int start, int stop)
        {
            return Start == start && Stop == stop;
        }

        public bool Equals(Range r)
        {
            return Start == r.Start && Stop == r.Stop;
        }

        public Range Intersection(Range range)
        {
            Range ret = new Range();

            if ((range.Stop < Start) || (range.Start >= Stop) || (Empty && range.Empty))
            {
                ret.Empty = true;
            }
            else if (range.Stop >= Start && range.Stop <= Stop && range.Start < Start)
            {
                ret.Empty = false;
                ret.Start = Start;
                ret.Stop = range.Stop;
                if (ret.Start == ret.Stop)
                {
                    ret.Empty = true;
                }
            }
            else if (range.Stop >= Start && range.Stop <= Stop)
            {
                ret.Empty = false;
                ret.Start = range.Start;
                ret.Stop = range.Stop;
                if (ret.Start == ret.Stop)
                {
                    ret.Empty = true;
                }
            }
            else if (range.Start >= Start && range.Start <= Stop)
            {
                ret.Empty = false;
                ret.Start = range.Start;
                ret.Stop = Stop;
                if (ret.Start == ret.Stop)
                {
                    ret.Empty = true;
                }
            }
            else if (Start >= range.Start && Stop <= range.Stop)
            {
                ret.Start = Start;
                ret.Stop = Stop;
                ret.Empty = false;
            }
            return ret;
        }

        public BigInteger Size
        {
            get { return Stop - Start;  }
        }
    }
}
